const mysql = require('mysql');

module.exports = (sql,params=[]) => {
    //1.创建连接对象
    let connection = mysql.createConnection({
        host     : 'localhost',
        user     : 'root',
        password : 'root',
        database : '01b_blog',
        port:3306
    })

    //2.连接数据库
    connection.connect((error) => {
        if(error){
            console.log("数据库连接失败")
        }else{
            console.log("数据库连接成功")
        }
    });

    //3.交互
    return new Promise((resolve,reject) => {
        connection.query(sql,params,(error,data) => {
            if(error){
                reject(error)
            }else{
                resolve(data)
            }
            //4.关闭
            connection.end();
        })
    })
}   